<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@ taglib prefix="shiro" uri="http://shiro.apache.org/tags"%>
<c:set var="ctx" value="${pageContext.request.contextPath}" />
<!DOCTYPE html>
<html lang="en">
<head>
<title>销售合同列表</title>
<meta http-equiv="content-type" content="text/html; charset=UTF-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<link rel="icon" href="favicon.ico" type="image/x-icon" />
<link rel="stylesheet" href="${ctx}/rs/css/bootstrap-table.css" />
<link rel="stylesheet" href="${ctx}/rs/css/main.css" />
<link id="theme" rel="stylesheet" href="${ctx}/rs/joli/css/theme-default.css" />
<style type="text/css">
.table th, .table td { 
text-align: center;
vertical-align: middle!important;
}
</style>
</head>
<body >

     <!-- START PAGE CONTAINER -->
      <div class="page-container">
        <!-- 引入左菜单部分 -->
          <jsp:include page="../joli/menu.jsp"></jsp:include>
          
          <!-- START PAGE CONTENT -->
          <div class="page-content">
              <jsp:include page="../joli/top.jsp"></jsp:include>
              
              <!-- START BREADCRUMB -->
              <ul class="breadcrumb">
                  <li><a href="#">合同管理</a></li>                    
                  <li class="active">销售合同</li>
              </ul>
              <!-- END BREADCRUMB -->   
              
              
              <!-- PAGE CONTENT WRAPPER -->
              <div class="page-content-wrap">
                 <form id="form2" method="post" action="${ctx}/contract/importExcel.htm">
                    <input type="file" id="phoneFile" style="width:0;height:0" name="phoneFile"  onchange="fileChange(this)"/>
                    <input type = "hidden" name = "type" value = "1">
                    </form>
                <div class="row">
                    <div class="col-xs-12">
                        <div id="toolbar">
                         
                <a class="fl btn btn-info srt-btn btn-sm icon-adds" onclick="add()">添加合同</a>
                <a class="fl btn btn-info srt-btn btn-sm icon-adds"  href="javascript:importPhone()" >excel批量添加</a>
                <a class="fl btn btn-info srt-btn btn-sm icon-adds" href="${ctx}/contract/showYBsell.htm">下载模板</a>
                <div class="fr pb10">
                          <select class="srt-put1 mr10 widput1" id="contractStatus" onchange="refresh()">
                                    <option value="">全部状态</option>
                                    <option value="0">未启动</option>
                                    <option value="1">执行中</option>
                                    <option value="2">结束</option>
                                </select>
                                <select class="srt-put1 mr10 widput1" id="contractsou" >
                                    <option value="0">合同编号</option>
                                    <option value="1">客户名称</option>
                                </select>
                                <input type="text" class="srt-put1 mr10 widput1" placeholder="请输入内容"
                                    id="key" name="key"/>
                                <span class="name">签约日期：</span>
                       <span class="time"><input type="text" id="beginTime" class="srt-put1 mr10 widput1" 
                                onfocus="WdatePicker()" />至 &nbsp; <input
                                type="text" id="endTime" class="srt-put1 mr10 widput1" 
                                onfocus="WdatePicker({minDate:'#F{$dp.$D(\'beginTime\')}'})" /></span> 
                                
                                
                                <button class="btn btn-primary btn-sm srt-btn active"
                                    onclick="refresh()">搜索</button>
                            </div>
                        </div>
                        <table id="table" cellpadding="0" cellspacing="0" data-toggle="table" data-toolbar="#toolbar"
                            data-url="${ctx}/contract/sellList.htm"
                            data-cache="false"
                            data-pagination="true" 
                            data-side-pagination="server"
                            data-page-list="[10, 20, 50, 100]"
                            data-page-size="20"  
                            data-query-params="queryParams">
                            <thead>
							     <tr class="tab-th">
							         <th data-field="contractCode" class="col-md-0.7">销售合同编号</th>
							         <th data-field="advName" class="col-md-1">广告客户</th>
							         <th data-field="signSumMoney" class="col-md-1" data-formatter="moneyFormatter">签约总金额</th>
							         <th data-field="copewithMoney" class="col-md-1" data-formatter="moneyFormatter">应付总金额</th>
							         <th data-field="addupMoney" class="col-md-1" data-formatter="moneyFormatter">累计已付金额</th>
							         <th data-field="nonPayMoney" class="col-md-1" data-formatter="moneyFormatter">未付金额</th>
							         <th data-field="throwMoney" class="col-md-1" data-formatter="moneyFormatter">媒体投放总金额</th>
							         <th data-field="signTime" class="col-md-1" data-formatter="signtimeFormatter">签约日期</th>
							         <th data-field="timeToTime" class="col-md-1" data-formatter="timetotimeFormatter">合同有效期</th>
							         <th data-field="contractStatus" class="col-md-0.5" data-formatter="statusFormatter">合同状态</th>
							         <th data-field="action" class="col-md-2.5" data-formatter="actionFormatter">操作</th>
							     </tr>
                            </thead>
                        </table>
                </div>
              </div>  
         </div>
     </div>
     </div>
          <!-- 引入所有joli的脚本 -->
    <jsp:include page="../joli/scripts.jsp"></jsp:include>
    <!-- 引入页面需要的脚本 -->
    <script type="text/javascript" src="${ctx}/rs/js/bootstrapQ.min.js"></script>
    <script type="text/javascript" src="${ctx}/rs/js/qiao.js"></script>
    <script src="${ctx}/rs/js/bootstrap-table.js"></script>
    <script src="${ctx}/rs/js/bootstrap-table-zh-CN.js"></script>
    <script src="${ctx}/rs/js/jquery.formatDate.js"></script>
    <script src="${ctx}/rs/js/jquery.form.js" type="text/javascript"></script>
    <script src="${ctx}/rs/js/my97DatePicker/WdatePicker.js"></script>
    <script type="text/javascript" src="${ctx}/rs/js/session-out-error.js"></script>
    <script type="text/javascript">
    activeMenu($("#menu_contract_0"));
    var $table = $('#table');
    
    $table.on('click-row.bs.table', function (e, row, $element) {
          $table.bootstrapTable('uncheckAll');
          $table.bootstrapTable('check', $element.index());
    });
    
    $table.bootstrapTable({
        height: getHeight()
    });
      
    //获取分辨率高度
      function getHeight() {
          return window.screen.height - 308;
      }
      
      $(window).resize(function () {
      $table.bootstrapTable('resetView',{height: getHeight()});
    });
      
    //初始化刷新
   function refresh(pageIndex,selectPage){
        if(!pageIndex && !selectPage){
            pageIndex = 0;
            selectPage = 0;
        }
        var key = encodeURIComponent($("#key").val());
        var contractsou = '';
        if (key != null || key != ""){
        	contractsou = $("#contractsou").val();
        }
        var contractStatus = $("#contractStatus").val();
        var beginTime = $("#beginTime").val();
        var endTime = $("#endTime").val();
        var pageSize = $table.bootstrapTable('getOptions').pageSize;
        pageIndex=pageIndex*pageSize;
        $table.bootstrapTable('refresh', {
            silent: true,
            cache : false,
            url: '${ctx}/contract/sellList.htm',
            query: {
                key:key,
                contractsou:contractsou,
                contractStatus:contractStatus,
                startTime:beginTime,
                endTime:endTime,
                pageIndex : pageIndex,
                pageSize : pageSize
            }
        });
        $table.bootstrapTable('selectPage', selectPage);
    }
   function queryParams(params) {
       var key = encodeURIComponent($("#key").val());
       var contractsou = '';
       if (key != null || key != ""){
           contractsou = $("#contractsou").val();
       }
       var contractStatus = $("#contractStatus").val();
       var beginTime = $("#beginTime").val();
       var endTime = $("#endTime").val();
       return {
    	   key:key,
           contractsou:contractsou,
           contractStatus:contractStatus,
           startTime:beginTime,
           endTime:endTime,
           pageIndex : params.offset,
           pageSize : params.limit
       };
   }
    function signtimeFormatter(value, row, index){
         if(value){
             return $.formatDate("yyyy-MM-dd", new Date(value));
         }
         return "--";
    }
   
    function timetotimeFormatter(value, row, index){
         if(value){
        	 var a = value.split("~");
        	 
             return a[0]+"<br/>"+"~"+a[1];
         }
         return "--";
    }
    function actionFormatter(value, row, index){
        var retbak="";
        retbak='<a class="mr15" href="javascript:adduplist('+row.id+')">已付款记录</a><a class="mr15" href="javascript:contractInfo('+row.id+')">合同详情</a><a class="mr15" href="javascript:throwInfo(\''+row.contractCode+'\')">媒体投放</a>';
        return retbak;
    }
    function statusFormatter(value, row, index){
        var retbak="";
        switch(row.contractStatus){
        case 0:
            retbak="<sapn class='label label-success'>未启动</span>";
            break;
        case 1:
            retbak="<span class='label label-info'>执行中</span>";
            break;
        case 2:
            retbak="<span class='label label-danger'>结束</span>";
            break;
        }
        return retbak;
    }
    function moneyFormatter(value, row, index){
       if(value == null){
    	   value="0.00";
    	}
    	return "￥"+value.toFixed(2);
    }
    
    
    function add() {
        var pageNumber = $table.bootstrapTable('getOptions').pageNumber;
        var pageIndex = pageNumber - 1;
        bootstrapQ.dialog({
            url : '${ctx}/contract/selladd.htm',
            title : '添加合同',
            width : "650px",
            scrolly : "true",
            close : 'true',
            hidebtn : 'true'
        },function(){
            //回调触发关闭窗口
            $("#bsmodal").modal("hide");
            refresh(pageIndex,pageNumber);
        });
    };
    
    
  //已付款记录
    function adduplist(id) {
             bootstrapQ.dialog({
                 url : "${ctx}/contract/adduplist.htm?id="+ id+"&type="+1,
                 title : "已付款记录",
                 width: '650px',
                 close : 'true',
                 hidebtn : 'true'
             },function(){
                 //回调触发关闭窗口
                 $("#bsmodal").modal("hide");
             });
     }
  //合同详情
    
					function contractInfo(id) {
						qiao2.bs.dialog({
							id: 'contractInfo',
							url : "${ctx}/contract/contractInfo.htm?id=" + id
									+ "&type=" + 1,
							title : "合同详情",
							width : '40%',
							close : 'true',
							hidebtn : 'true',
							foot: false
						}, function() {
							//回调触发关闭窗口
							$("#bsmodal").modal("hide");
							refresh();
						});
					}
					//媒体投放
					function throwInfo(contractCode) {
						qiao2.bs.dialog({
							 id: 'mediaThrow',
							url : "${ctx}/contract/sellthrowInfo.htm?contractCode=" + contractCode,
							title : "媒体投放采购合同",
							width : '800px',
							close : 'true',
							hidebtn : 'true',
							foot: false
						}, function() {
							//回调触发关闭窗口
							$("#bsmodal").modal("hide");
							refresh();
						});
					}

					function importPhone() {
						$("#phoneFile").click();
					}

					function fileChange(file) {
						var fileName = $("#phoneFile").val();
						var str = fileName.substr(fileName.lastIndexOf("."))
								.toLowerCase();
						if (str != '.xls' && str != '.xlsx') {
							bootstrapQ.msg({
								msg : '请选择Excel文件(格式为xls)',
								type : 'danger',
								time : 2000
							});
							//重置文件选择器
							$("#form2").resetForm();
							return;
						}
						$("button.bsok").click();
						var optionForm2 = {
							dataType : "json",
							success : function(result) {
								//重置文件选择器
								$("#form2").resetForm();
								if (!$('.unable').is(':hidden')) {
									$(".unable").hide();
								}
								if (result.responseText == null
										|| result.responseText == '') {
									bootstrapQ.msg({
										msg : '导入成功！',
										type : 'success',
										time : 2000
									});
								} else {
									bootstrapQ.msg({
										msg : result.responseText,
										type : 'danger',
										time : 2000
									});
								}
								refresh();
							},
							error : function(result) {
								//重置文件选择器
								$("#form2").resetForm();
								if (!$('.unable').is(':hidden')) {
									$(".unable").hide();
								}
								if (result.responseText == null
										|| result.responseText == '') {
									bootstrapQ.msg({
										msg : '导入成功！',
										type : 'success',
										time : 2000
									});
								} else {
									bootstrapQ.msg({
										msg : result.responseText,
										type : 'danger',
										time : 3000
									});
									//                     bootstrapQ.alert(result.responseText);
								}
								refresh();
							}
						};
						$("#form2").ajaxSubmit(optionForm2);
					}
				</script>
  </body >
</html>
